let fs = require('fs');
let moment = require('moment');
moment.locale('zh-cn');
let ejsExcel = require('ejsexcel');

const inputFileName = 'template-3.xlsx';
const outputFileName = 'template-3.output.xlsx';


//获得Excel模板的buffer对象
let inputBuffer = fs.readFileSync(`./${inputFileName}`);

//数据源
let data = {
    'projectName': '测试项目',
    'propertyStatus': '商业',
    'completionDate': '2018-02-12',
    'buildingNumber': '1',
    'checkStage': '第一阶段',
    'checkStartDate': '2017-11-01',
    'checkEndDate': '2017-12-10',
    'developers': '某某建设公司',
    'supervisorUnit': '某某监理单位',
    'constructionUnit': '某某施工单位',
    'propertyManagement': '某某项目物管处',
    'reportItems': [
        {
            'buildingName': '1号楼',
            'details': [
                {
                    'typeName': '防盗门',
                    'checkDiscoverCount': 103,
                    'reviewRectifyCount': 100,
                    'newProblemCount': 2,
                    'sort': 1
                },
                {
                    'typeName': '门窗、玻璃、百叶',
                    'checkDiscoverCount': 104,
                    'reviewRectifyCount': 101,
                    'newProblemCount': 3,
                    'sort': 2
                },
                {
                    'typeName': '铁艺栏杆、护栏',
                    'checkDiscoverCount': 105,
                    'reviewRectifyCount': 102,
                    'newProblemCount': 4,
                    'sort': 3
                },
                {
                    'typeName': '建筑防水',
                    'checkDiscoverCount': 106,
                    'reviewRectifyCount': 103,
                    'newProblemCount': 5,
                    'sort': 4
                },
                {
                    'typeName': '室内安装',
                    'checkDiscoverCount': 107,
                    'reviewRectifyCount': 104,
                    'newProblemCount': 6,
                    'sort': 5
                },
                {
                    'typeName': '土建',
                    'checkDiscoverCount': 108,
                    'reviewRectifyCount': 105,
                    'newProblemCount': 7,
                    'sort': 6
                },
                {
                    'typeName': '智能化',
                    'checkDiscoverCount': 109,
                    'reviewRectifyCount': 106,
                    'newProblemCount': 8,
                    'sort': 7
                },
                {
                    'typeName': '设计问题',
                    'checkDiscoverCount': 110,
                    'reviewRectifyCount': 107,
                    'newProblemCount': 9,
                    'sort': 8
                }
            ]
        }
    ],
    'rectifyRate': 76,
    'outputRatio': 69,
    'acceptanceTotalNumber': 240,
    'problemNewNumber': 480,
    'averageNumber': 2,
    'remark': ''
};

var compute = [
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0},
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0},
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0},
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0},
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0},
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0},
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0},
    {'sumCDC': 0, 'sumRRC': 0, 'sumNPC': 0}
];

let items = data.reportItems;
for (let i = 0; i < items.length; i++) {
    let details = items[i].details;
    for (let k = 0; k < details.length; k++) {
        compute[k].sumCDC = compute[k].sumCDC + parseInt(details[k].checkDiscoverCount, 10);
        compute[k].sumRRC = compute[k].sumRRC + parseInt(details[k].reviewRectifyCount, 10);
        compute[k].sumNPC = compute[k].sumNPC + parseInt(details[k].newProblemCount, 10);
    }
}
data.compute = compute;


data.completionDateFormat = moment(data.completionDate).format('YYYY年MM月DD日');
data.checkDateFormat = `${moment(data.checkStartDate).format('YYYY年MM月DD日')} - ${moment(data.checkEndDate).format('YYYY年MM月DD日')}`;
data.newProblems = `一共验收 ${data.acceptanceTotalNumber} 户，复查时新增 ${data.problemNewNumber} 条，平均每户 ${data.averageNumber} 条。`;


//用数据源(对象)data渲染Excel模板
ejsExcel.renderExcel(inputBuffer, data).then(function (outputBuffer) {
    fs.writeFileSync(`./${outputFileName}`, outputBuffer);
    console.log(`生成成功：${outputFileName}`);
}).catch(function (error) {
    console.error(`错误：${error}`);
});
